package top.lixb.libcommon.base;


import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.vondear.rxui.view.dialog.RxDialogLoading;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import top.lixb.libcommon.R;
import top.lixb.libcommon.net.CommonCallback;
import top.lixb.libcommon.net.INet;
import top.lixb.libcommon.net.NetApiHelper;

public abstract class BaseFragment<T extends ViewDataBinding> extends Fragment implements INet {

    protected RxDialogLoading mRxDialogLoading;
    protected T mViewBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mViewBinding = DataBindingUtil.inflate(inflater, getLayout(), container, false);
        View view = null;
        if (null == mViewBinding) {
            view = inflater.inflate(getLayout(), container, false);
        } else {
            view = mViewBinding.getRoot();
        }
//        View view = inflater.inflate(getLayout(), container, false);
//        initView(view);
//        mViewBinding = DataBindingUtil.bind(view);
        initView(view);
        return view;
    }


    @Override
    public void performGetRequest(String baseUrl, String url, Map<String, String> params, CommonCallback callback) {
        NetApiHelper.createNetApi(baseUrl)
                .commonGetRequest(url, params, NetApiHelper.getCommonHeader())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callback)
        ;
    }

    @Override
    public void performPostRequest(String baseUrl,String url, Map<String, String> params, CommonCallback callback) {
        NetApiHelper.createNetApi(baseUrl)
                .commonPostRequest(url, params, NetApiHelper.getCommonHeader())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callback)
        ;
    }

    @Override
    public void onStop() {
        super.onStop();
        hideLoading();
    }

    protected abstract void initView(View view);

    protected abstract int getLayout();

    protected RxDialogLoading getLoadingDialog() {
        if (null == mRxDialogLoading) {
            mRxDialogLoading = new RxDialogLoading(getActivity());
            mRxDialogLoading.setContentView(R.layout.dialog_loading);
        }
        return mRxDialogLoading;
    }
    protected void showLoading() {
        if (!getLoadingDialog().isShowing()) {
            getLoadingDialog().show();
        }
    }

    protected void hideLoading() {
        if (getLoadingDialog().isShowing()) {
            getLoadingDialog().dismiss();
        }
    }
}
